package com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzxczh.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzxczh.base.dict.service.DictService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.service.PersonnelManagementService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.vo.ImportExcelVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.zcpt.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Component
public class ExcelVerifyHandler implements IExcelVerifyHandler<ImportExcelVo> {

    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    DictService dictService;
    @Autowired
    EarEveManageService earEveManageService;
    @Autowired
    PersonnelManagementService personnelManagementService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(ImportExcelVo importExcelVo) {

        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");
        try {
            if (!PlatformObjectUtils.isEmpty(importExcelVo.getProvince())) {
                if(checkDivision(importExcelVo.getProvince(),"","")){
                    result.setSuccess(false);
                    message.append("省名称 不符合规范！");
                }else{
                    if (!PlatformObjectUtils.isEmpty(importExcelVo.getCity())) {
                        if(checkDivision(importExcelVo.getProvince(),importExcelVo.getCity(),"")){
                            result.setSuccess(false);
                            message.append("市名称 不符合规范！");
                        }else{
                            if (!PlatformObjectUtils.isEmpty(importExcelVo.getCounty())) {
                                if(checkDivision(importExcelVo.getProvince(),importExcelVo.getCity(),importExcelVo.getCounty())){
                                    result.setSuccess(false);
                                    message.append("县名称 不符合规范！");
                                }

                            }
                        }

                    }
                }
            } else {
                message.append("省 不能为空！");
            }


            if (PlatformObjectUtils.isEmpty(importExcelVo.getPersonName())) {
                result.setSuccess(false);
                message.append("姓名 不能为空！");
            }

            if (PlatformObjectUtils.isEmpty(importExcelVo.getCategory())) {
                result.setSuccess(false);
                message.append("类别 不能为空！");
            } else {
                //校验导入的数据是否规范,导入的值数据字典中是否存在
                String structureType = importExcelVo.getCategory();
                List<String> list = checkData("rylb");
                if (!list.contains(structureType)) {
                    result.setSuccess(false);
                    message.append("类别 不正确！");
                }
            }


            if (PlatformObjectUtils.isEmpty(importExcelVo.getRole())) {
                result.setSuccess(false);
                message.append("角色 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(importExcelVo.getUnit())) {
                result.setSuccess(false);
                message.append("单位名称 不能为空！");
            }
            if (PlatformObjectUtils.isEmpty(importExcelVo.getTel())) {
                result.setSuccess(false);
                message.append("手机号 不能为空！");
            } else {
                EarEveManage currentEvent = earEveManageService.getCurrentEvent();//获取当前地震事件
                PersonnelManagement personnelManagement = personnelManagementService.findByXsemIdAndTel(importExcelVo.getTel(), currentEvent.getXsemId());
                if (!PlatformObjectUtils.isEmpty(personnelManagement)) {
                    result.setSuccess(false);
                    message.append("手机号 " + importExcelVo.getTel() + "重复！");
                }
            }

        } catch (Exception e) {

        }
        result.setMsg(message.toString());
        return result;
    }

    private Boolean checkDivision(String province,String city,String area) {
        Boolean flag=false;
        String idByCondition = null;
        try {
            idByCondition = sysAreaService.getIdByCondition(URLEncoder.encode(province, "utf-8"), URLEncoder.encode(city, "utf-8"), URLEncoder.encode(area, "utf-8"));

        if(!PlatformObjectUtils.isEmpty(idByCondition)){
            String[] s = idByCondition.split("_");
            if(!PlatformObjectUtils.isEmpty(province)){
                if(PlatformObjectUtils.isEmpty(s[0])){
                    flag=true;
                }
            }
            if(!PlatformObjectUtils.isEmpty(province) && !PlatformObjectUtils.isEmpty(city)){
                if(PlatformObjectUtils.isEmpty(s[0])){
                    flag=true;
                }
                if(PlatformObjectUtils.isEmpty(s[1])){
                    flag=true;
                }
            }
            if(!PlatformObjectUtils.isEmpty(province) && !PlatformObjectUtils.isEmpty(city) && !PlatformObjectUtils.isEmpty(area)){
                if(PlatformObjectUtils.isEmpty(s[0])){
                    flag=true;
                }
                if(PlatformObjectUtils.isEmpty(s[1])){
                    flag=true;
                }
                if(PlatformObjectUtils.isEmpty(s[2])){
                    flag=true;
                }
            }
        }

        } catch (Exception e) {
            flag=true;
        }
        return flag;
    }

    private List<String> checkData(String code){
        List<DictItemEntity> validDictItemsByDictCode = dictService.getValidDictItemsByDictCode(code);
        List<String> list = new ArrayList<>();
        for(DictItemEntity dictItemEntity: validDictItemsByDictCode){
            String dictItemName = dictItemEntity.getDictItemName();
            list.add(dictItemName);
        }
        return list;
    }


}
